home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 1996 April / Software of the Month Club 1996 April.iso / pc / os2 / psutils / src / epsffit.c < prev    next >
C/C++ Source or Header  |  1996-02-21  |  6KB  |  192 lines

  1. /* epsffit.c
  2.  * Copyright (C) Angus J. C. Duggan 1991-1995
  3.  * See file LICENSE for details.
  4.  *
  5.  * fit epsf file into constrained size
  6.  * Usage:
  7.  *       epsffit [-c] [-r] [-a] [-s] llx lly urx ury [infile [outfile]]
  8.  *               -c centres the image in the bounding box given
  9.  *               -r rotates the image by 90 degrees anti-clockwise
  10.  *               -a alters the aspect ratio to fit the bounding box
  11.  *               -s adds a showpage at the end of the image
  12.  *
  13.  * Added filename spec (from Larry Weissman) 5 Feb 93
  14.  * Accepts double %%BoundingBox input, outputs proper BB, 4 Jun 93. (I don't
  15.  * like this; developers should read the Big Red Book before writing code which
  16.  * outputs PostScript.
  17.  */
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <ctype.h>
  23.  
  24. #include "pserror.h"
  25. #include "patchlev.h"
  26. #include "config.h"
  27.  
  28. #define MIN(x,y) ((x) > (y) ? (y) : (x))
  29. #define MAX(x,y) ((x) > (y) ? (x) : (y))
  30.  
  31. char *program;
  32.  
  33. static void usage(void)
  34. {
  35.    fprintf(stderr, "%s release %d patchlevel %d\n", program, RELEASE, PATCHLEVEL);
  36.    fprintf(stderr, "Copyright (C) Angus J. C. Duggan, 1991-1995. See file LICENSE for details.\n");
  37.    fprintf(stderr, "Usage: %s [-c] [-r] [-a] [-s] llx lly urx ury [infile [outfile]]\n",
  38.        program);
  39.    exit(1);
  40. }
  41.  
  42. void main(int argc, char **argv)
  43. {
  44.    int bbfound = 0;              /* %%BoundingBox: found */
  45.    int urx, ury, llx, lly;
  46.    int furx, fury, fllx, flly;
  47.    int showpage = 0, centre = 0, rotate = 0, aspect = 0, maximise = 0;
  48.    char buf[BUFSIZ];
  49.    FILE *input;
  50.    FILE *output;
  51.  
  52.    program = *argv++; argc--;
  53.  
  54.    while (argc > 0 && argv[0][0] == '-') {
  55.       switch (argv[0][1]) {
  56.       case 'c': centre = 1; break;
  57.       case 's': showpage = 1; break;
  58.       case 'r': rotate = 1; break;
  59.       case 'a': aspect = 1; break;
  60.       case 'm': maximise = 1; break;
  61.       case 'v':
  62.       default:  usage();
  63.       }
  64.       argc--;
  65.       argv++;
  66.    }
  67.  
  68.    if (argc < 4 || argc > 6) usage();
  69.    fllx = atoi(argv[0]);
  70.    flly = atoi(argv[1]);
  71.    furx = atoi(argv[2]);
  72.    fury = atoi(argv[3]);
  73.  
  74.    if (argc > 4) {
  75.       if(!(input = fopen(argv[4], OPEN_READ)))
  76.      message(FATAL, "can't open input file %s\n", argv[4]);
  77.    }
  78. #if defined(MSDOS) || defined(WINNT)
  79.    else {
  80.       int fd = fileno(stdin) ;
  81.       if ( setmode(fd, O_BINARY) < 0 )
  82.          message(FATAL, "can't reset stdin to binary mode\n");
  83.       input = stdin ;
  84.     }
  85. #endif
  86.  
  87.    if (argc > 5) {
  88.       if(!(output = fopen(argv[5], OPEN_WRITE)))
  89.      message(FATAL, "can't open output file %s\n", argv[5]);
  90.    }
  91. #if defined(MSDOS) || defined(WINNT)
  92.    else {
  93.       int fd = fileno(stdout) ;
  94.       if ( setmode(fd, O_BINARY) < 0 )
  95.          message(FATAL, "can't reset stdout to binary mode\n");
  96.       output = stdout ;
  97.     }
  98. #endif
  99.  
  100.    while (fgets(buf, BUFSIZ, input)) {
  101.       if (buf[0] == '%' && (buf[1] == '%' || buf[1] == '!')) {
  102.      /* still in comment section */
  103.      if (!strncmp(buf, "%%BoundingBox:", 14)) {
  104.         double illx, illy, iurx, iury;    /* input bbox parameters */
  105.         if (sscanf(buf, "%%%%BoundingBox:%lf %lf %lf %lf\n",
  106.                &illx, &illy, &iurx, &iury) == 4) {
  107.            bbfound = 1;
  108.            llx = (int)illx;    /* accept doubles, but convert to int */
  109.            lly = (int)illy;
  110.            urx = (int)(iurx+0.5);
  111.            ury = (int)(iury+0.5);
  112.         }
  113.      } else if (!strncmp(buf, "%%EndComments", 13)) {
  114.         strcpy(buf, "\n"); /* don't repeat %%EndComments */
  115.         break;
  116.      } else fputs(buf, output);
  117.       } else break;
  118.    }
  119.  
  120.    if (bbfound) { /* put BB, followed by scale&translate */
  121.       int fwidth, fheight;
  122.       double xscale, yscale;
  123.       double xoffset = fllx, yoffset = flly;
  124.       double width = urx-llx, height = ury-lly;
  125.  
  126.       if (maximise)
  127.      if ((width > height && fury-flly > furx-fllx) ||
  128.          (width < height && fury-flly < furx-fllx)) 
  129.         rotate = 1;
  130.  
  131.       if (rotate) {
  132.      fwidth = fury - flly;
  133.      fheight = furx - fllx;
  134.       } else {
  135.      fwidth = furx - fllx;
  136.      fheight = fury - flly;
  137.       }
  138.  
  139.       xscale = fwidth/width;
  140.       yscale = fheight/height;
  141.  
  142.       if (!aspect) {       /* preserve aspect ratio ? */
  143.      xscale = yscale = MIN(xscale,yscale);
  144.       }
  145.       width *= xscale;     /* actual width and height after scaling */
  146.       height *= yscale;
  147.       if (centre) {
  148.      if (rotate) {
  149.         xoffset += (fheight - height)/2;
  150.         yoffset += (fwidth - width)/2;
  151.      } else {
  152.         xoffset += (fwidth - width)/2;
  153.         yoffset += (fheight - height)/2;
  154.      }
  155.       }
  156.       fprintf(output, 
  157.           "%%%%BoundingBox: %d %d %d %d\n", (int)xoffset, (int)yoffset,
  158.          (int)(xoffset+(rotate ? height : width)),
  159.          (int)(yoffset+(rotate ? width : height)));
  160.       if (rotate) {  /* compensate for original image shift */
  161.      xoffset += height + lly * yscale;  /* displacement for rotation */
  162.      yoffset -= llx * xscale;
  163.       } else {
  164.      xoffset -= llx * xscale;
  165.      yoffset -= lly * yscale;
  166.       }
  167.       fputs("%%EndComments\n", output);
  168.       if (showpage)
  169.      fputs("save /showpage{}def /copypage{}def /erasepage{}def\n", output);
  170.       else
  171.      fputs("%%BeginProcSet: epsffit 1 0\n", output);
  172.       fputs("gsave\n", output);
  173.       fprintf(output, "%.3f %.3f translate\n", xoffset, yoffset);
  174.       if (rotate)
  175.      fputs("90 rotate\n", output);
  176.       fprintf(output, "%.3f %.3f scale\n", xscale, yscale);
  177.       if (!showpage)
  178.      fputs("%%EndProcSet\n", output);
  179.    }
  180.    do {
  181.       fputs(buf, output);
  182.    } while (fgets(buf, BUFSIZ, input));
  183.    if (bbfound) {
  184.       fputs("grestore\n", output);
  185.       if (showpage)
  186.      fputs("restore showpage\n", output); /* just in case */
  187.    } else
  188.       message(FATAL, "no %%%%BoundingBox:\n");
  189.  
  190.    exit(0);
  191. }
  192.